<?php

include_once("space.class.php");
$space = new space();

require MOD_ROOT.'attachment/attachment.class.php';
$attachment = new attachment($module);

if(QUERY_STRING && !isset($userid) && !isset($username)) $userid = intval(QUERY_STRING);
if(!$forward) $forward = HTTP_REFERER;


if(!$userid && $prefix){
	$result = $db->get_one("select `userid` from ".DB_PRE."member where prefix = '$prefix'");
	$userid = $result['userid'];
	unset($result);
}
else{
	$userid = isset($userid) ? intval($userid) : $_userid;
}


$member_api = load('member_api.class.php', 'member');

if(isset($username) && !empty($username))
{
	$userid = $member_api->get_userid($username);
}
if(!$userid)
{
	$userid = $_userid;
}
else
{
	$userid = intval($userid);
}
$is_host = (isset($_userid) && ($userid == $_userid)) ? 1 : 0;
if($userid < 1) showmessage('请选择你想查看的用户', '?module=member&action=list&modelid=10');


$result = $member_api->get($userid, array('userid', 'disabled'));
if(!$result) showmessage('所查看的用户不存在');
if($result['disabled']) showmessage('该用户已被禁用', $forward);


$memberinfo = $member_api->get($userid, array('username', 'groupid', 'lastlogintime', 'intro'));
//$memberinfo['avatar'] = avatar($userid);
@extract(new_htmlspecialchars($memberinfo));
unset($memberinfo);


switch ($action)
{

	case 'blog':
	 
		if($userid < 1) return false;
		/*$memberinfo = $member->get($userid, $fields = '*', 1);
		$memberinfo['avatar'] = avatar($userid);
		@extract(new_htmlspecialchars($memberinfo));
		$spaceinfo = $space->get_space($userid);
		@extract(new_htmlspecialchars($spaceinfo));
		unset($spaceinfo);
		$head['title'] = !empty($spacename) ? $spacename: $username.$LANG['personal_space'];*/
		
		if(!class_exists('blog'))
		{
			if(file_exists(MOD_ROOT.'blog/blog.class.php')){
				require MOD_ROOT.'blog/blog.class.php';
			}
			else{
				require WWW_ROOT.'sites/modules/blog/blog.class.php';
			}
		}
		$blog = new blog();
		$where = " `status`=99 AND `userid` = '$userid' "; 
		$blogs = $blog->listinfo($where, '`listorder` DESC,`blogid` DESC', $page, 20);
		$pages = $blog->pages;
		include template('blog.html','space');
	break;
	
	case 'contribute':
	 
		if($userid < 1) return false;
		/*$memberinfo = $member->get($userid, $fields = '*', 1);
		$memberinfo['avatar'] = avatar($userid);
		@extract(new_htmlspecialchars($memberinfo));
		$spaceinfo = $space->get_space($userid);
		@extract(new_htmlspecialchars($spaceinfo));
		unset($spaceinfo);
		$head['title'] = !empty($spacename) ? $spacename: $username.$LANG['personal_space'];*/
		
		if(!class_exists('c'))
		{
			require_once MOD_ROOT.'content/content.class.php';
		}
		$c = new content();
		$where = " `status`=99 AND `userid` = '$userid' "; 
		$data = $c->listinfo($where, '`listorder` DESC,`contentid` DESC', $page, 20);
		$pages = $c->pages;
		include template('contribute.html','space');
	break;
	
	case 'profile':
		if(!$forward) $forward = HTTP_REFERER;
		if($userid < 1) return false;
		$is_host = ($userid == $_userid) ? 1 : 0;
		$memberinfo = $member->get($userid, '*', 1);
		if($memberinfo['disabled']) showmessage('该用户已被禁用', $forward);
		if(!$memberinfo) showmessage('请选择有效用户', $forward);
		if(!class_exists('member_output'))
		{
			require CACHE_MODEL_PATH.'member_output.class.php';
		}
		
		@extract(new_htmlspecialchars($memberinfo));
		$data = $member->get_model_info($userid, $modelid);
		$member_output = new member_output($modelid, $userid);
		$forminfos = $member_output->get($data);
		if($userid != $_userid)
		{
			$models = cache_read($modelid.'_fields.inc.php', CACHE_MODEL_PATH);
			foreach($forminfos as $f)
			{
				if($models[$f]['isshow']!=1) unset($forminfos[$f]);
			}
		}
		$avatar = avatar($userid);
		if($regtime) $regtime = date('Y-m-d H:m:i', $regtime);
		if($lastlogintime) $lastlogintime = date('Y-m-d H:m:i', $lastlogintime);
		
		unset($memberinfo);
		include template('profile.html','space');
	break;
	
	case 'tweet':
		if($userid < 1) return false;
		
		if(!class_exists('tweet'))
		{
			if(file_exists(MOD_ROOT.'tweet/tweet.class.php')){
				require MOD_ROOT.'tweet/tweet.class.php';
			}
			else{
				require WWW_ROOT.'sites/modules/tweet/tweet.class.php';
			}
		}
		$tweet = new tweet();
		$where = " `status`=99 AND `userid` = '$userid' "; 
		$data = $tweet->listinfo($where, '`listorder` DESC,`tweetid` DESC', $page, 20);
		$pages = $tweet->pages; 
		include template('tweet.html','space');
	break;


	default:
		/*增加来访记录*/
		include MOD_ROOT.'member/includes/visitor.class.php';
		$visitor = new visitor();
		$visitor->add($userid);
		
		$visitors = $visitor->listinfo(" userid = $userid");
		
		
		$spaceinfo = $space->get_space($userid);
		@extract(new_htmlspecialchars($spaceinfo));
		unset($spaceinfo);
		
		
		require_once MOD_ROOT.'content/content.class.php';
		$c = new content();
		$c->set_userid($userid);
		/*发布的文章*/
		$contents = $c->listinfo("userid=$userid", '`listorder` DESC,`contentid` DESC', $page, 2);
		
		// 博客
		if(!class_exists('blog'))
		{
			if(file_exists(MOD_ROOT.'blog/blog.class.php')){
				require MOD_ROOT.'blog/blog.class.php';
			}
			else{
				require WWW_ROOT.'sites/modules/blog/blog.class.php';
			}
		}
		$blog = new blog();
		$blogs = $blog->listinfo("userid=$userid", '`listorder` DESC,`blogid` DESC', $page, 2);
		
		// 关注TA的人
		$fans = $db->select("select * from ".DB_PRE."member_fans f , ".DB_PRE."member m where f.fansid = m.userid and f.`userid` = $userid");
		
		// TA关注的人
		$follows = $db->select("select * from ".DB_PRE."member_fans f , ".DB_PRE."member m where f.userid = m.userid and f.`fansid` = $userid");
		
		// 小组
		$groups = $db->select("select * from ".DB_PRE."group g , ".DB_PRE."group_member m where g.groupid = m.groupid and m.`userid` = $userid limit 0,8");
		
		$head['title'] = !empty($spacename) ? $spacename: $username.$LANG['personal_space'];
		$head['keywords'] = $SETTING['meta_keywords'];
		$head['description'] = $SETTING['meta_description'];
		if(!$spacename) $spacename = $username;
		
		include template('index.html','space');
	
	break;
}
?>